require(['jquery', 'underscore', 'echarts', 'echarts/config', 'echarts/chart/force','echarts/chart/chord'],
function($, _, ec, ecConfig) {
    var container = $('#lineage-container');
    container.height(window.screen.availHeight - 180);
    var myChart = ec.init(container[0]);
    var categoryObj={
    	'MySQLTable':'MySQL表',
    	'MySQLDatabase':'MySQL数据库',
    	'MySQLColumn':'MySQL列',
    	'MongoDBDatabase':'MongoDB数据库',
    	'MongoDBCollection':'MongoDB集合',
    	'KettleTransform':'Kettle任务',
    	'KettleStep':'Kettle步骤',
    };
    var categoryList=_.map(categoryObj,function(v){return v});
    var categories=_.map(categoryList,function(v){return {name:v};});
    var ji=0;
    var categoryMap=_.mapObject(categoryObj,function(v,i){return ji++;});
    var option = {
        tooltip: {
            trigger: 'item',
            formatter: '{a} : {b}'
        },
        toolbox: {
            show: true,
            feature: {
                restore: {
                    show: true
                },
                magicType: {
                    show: true,
                    type: ['force', 'chord']
                },
                saveAsImage: {
                    show: true
                }
            }
        },
        legend: {
            x: 'left',
            data: categoryList,
        },
        series: [{
            type: 'force',
            //name: "关系",
            ribbonType: false,
            categories: categories,
            sort: 'ascending',
            sortSub: 'descending',
            itemStyle: {
                normal: {
                    label: {
                        show: true,
                        textStyle: {
                            color: '#333'
                        }
                    },
                    nodeStyle: {
                        brushType: 'both',
                        borderColor: 'rgba(255,215,0,0.4)',
                        borderWidth: 1
                    }
                },
                emphasis: {
                    label: {
                        show: false
                        // textStyle: null      // 默认使用全局文本样式，详见TEXTSTYLE
                    },
                    nodeStyle: {
                        //r: 30
                    },
                    linkStyle: {}
                }
            },
            minRadius: 15,
            maxRadius: 25,
            gravity: 1.1,
            scaling: 1.2,
            draggable: true,
            linkSymbol: 'arrow',
            steps: 10,
            coolDown: 0.9,

            //preventOverlap: true,
            
        }]
    };
    $.getJSON('/entity/globallineage', function(res) {
    	var nodes=[];
        var links=[];
        _.each(res.entities, function(e, i) {
    		var classname = e._classname.split('.');
    		classname = classname[classname.length-1];
    		nodes.push({category:categoryMap[classname], name:e._id, label:e.name});
            if (e.type == 'Table' && e.databaseId) {
                links.push({source : e._id, target : e.databaseId, name: 'belongTo'});
            } else if (e.type == 'Column' && e.tableId) {
                links.push({source: e._id, target: e.tableId, name: 'belongTo'});
            } else if (e.type == 'Step' && e.transformId) {
                links.push({source: e._id, target: e.transformId, name: 'belongTo'});
            }
    	});

    	_.each(res.relations, function(e, i) {
            links.push({source : e.fromId, target : e.toId , name: e.label});
    	});

        option.series[0].nodes = nodes;
        option.series[0].links = links;
    	myChart.setOption(option);
        myChart.on(ecConfig.EVENT.DBLCLICK, function (param){
            if (!param) {
                return;
            }
            // 点击links会跳转到一个暂时没有的页面，之后可以作为局部血统
            var detailUrl = param.data.category ? "detail.html?eid="+param.name : "lineage.html?source="+param.data.source+"&target="+param.data.target;

            window.location.href = detailUrl;
        });
    });
});

